package com.huluobo.a2202mvvm_im_douyindemo.viewDemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by LC on 2024/4/23.
 */
public class MyView extends View {

    private int progress = 0;
    private String timeString;

    //只在Java中new出一个View的时候使用
    // ImageView image = new ImageView(getContext());
    public MyView(Context context) {
        super(context);
    }

    //2个参数构造方法一般用在xml当中,所以要使用一个View,直接创建2个参数的构造方法即可
    //AttributeSet 属性集合
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private Paint paint;//画笔
    private Paint paintText;//绘制文字的笔

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);//设置绘制线
        paint.setStrokeWidth(5);//设置线的宽度
        paint.setAntiAlias(true);//消除锯齿

        paintText = new Paint();
        paintText.setColor(Color.BLUE);
        paintText.setTextSize(200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //给圆限定一个范围,距离LTRB 都是距离顶部或者左侧的距离,所以这个矩形是个500*500的正方形
        //其中的圆就是一个直径为500的圆 半径就是250
        RectF rectF = new RectF(100, 100, 600, 600);

        //参数: 范围,起始角度,终点角度,是否使用圆心,画笔
        canvas.drawArc(rectF, 270, progress, false, paint);
        canvas.drawText(timeString, 300, 410, paintText);
    }

    public void start(int time) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);//圆角变化的值
        valueAnimator.addUpdateListener(animation -> {
            progress = (int) animation.getAnimatedValue();
            invalidate();//view刷新自身的方法,相当于刷新适配器
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setDuration(time * 1000L);
        valueAnimator.start();

        ValueAnimator valueAnimatorText = ValueAnimator.ofInt(time, 0);//倒数,参数给了几秒就从几数到0
        valueAnimatorText.addUpdateListener(animation -> {
            timeString = String.valueOf(animation.getAnimatedValue());
            invalidate();
        });
        valueAnimatorText.setInterpolator(new LinearInterpolator());
        valueAnimatorText.setDuration(time * 1000L);
        valueAnimatorText.start();

        valueAnimatorText.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //当动画结束时,通过接口通知外部已经结束
                //4.在需要的位置回调出接口的方法
                onAnimEndListener.onAnimEnd();
            }
        });
    }

    //接口回调
    //1.创建一个接口
    public interface OnAnimEndListener {
        void onAnimEnd();
    }

    //2.声明接口,让接口在当前类中可用
    private OnAnimEndListener onAnimEndListener;

    //3.给接口声明一个set方法
    public void setOnAnimEndListener(OnAnimEndListener onAnimEndListener) {
        this.onAnimEndListener = onAnimEndListener;
    }
}
